package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Constants;
import com.bjpowernode.crm.commons.domain.ResultEntity;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.ActivityRemark;
import com.bjpowernode.crm.workbench.service.ActivityRemarkService;
import com.bjpowernode.crm.workbench.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 郭俊才
 * 2021/8/22
 */
@Controller
public class ActivityRemarkController {
    @Autowired
    private ActivityRemarkService activityRemarkService;
    @Autowired
    private UserService userService;
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/workbench/activity/getActivityRemarks.do")
    public String getActivityRemarks(Model model,String activityId){
        Activity activity = activityService.queryByPrimaryKeyForDetails(activityId);
        model.addAttribute("activity",activity);
        List<ActivityRemark> remarkList = activityRemarkService.queryByConditions(new ActivityRemark(null, null, null, null, null, null, null, activityId) );
        model.addAttribute("remarkList",remarkList);
       return "workbench/activity/detail";
    }

    //添加
    @ResponseBody
    @RequestMapping("/workbench/actity/createActivityRemark.do")
    public ResultEntity createActivityRemark(ActivityRemark activityRemark, HttpSession session){
        User user = (User) session.getAttribute(Constants.MESSAGE_SESSION_NAME);
        activityRemark.setCreateBy(user.getId());
        int i = activityRemarkService.saveSelective(activityRemark);
        if(i > 0){
            return ResultEntity.successWithoutData();
        }else {
            return ResultEntity.failed("添加失败");
        }
    }

    @ResponseBody
    @RequestMapping("/workbench/actity/dropActivityRemark.do")
    public ResultEntity dropActivityRemark(String id){
        int i = activityRemarkService.removeByPrimaryKey(id);
        if(i > 0){
            return ResultEntity.successWithoutData();
        }else {
            return ResultEntity.failed("删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("/workbench/actity/changeActivityRemark.do")
    public ResultEntity changeActivityRemark(ActivityRemark activityRemark, HttpSession session){
        //设置修改人
        User user = (User) session.getAttribute(Constants.MESSAGE_SESSION_NAME);
        activityRemark.setEditBy(user.getId());
        int i = activityRemarkService.editByPrimaryKeySelective(activityRemark);
        if(i > 0){
            return ResultEntity.successWithoutData();
        }else {
            return ResultEntity.failed("修改失败");
        }
    }
    @RequestMapping("/workbench/actity/getActivityRemark.do")
    @ResponseBody
    public ResultEntity getActivityRemark(String id){
        ActivityRemark activityRemark = activityRemarkService.queryByPrimaryKey(id);
        if(activityRemark != null){
            return ResultEntity.successWithData(activityRemark);
        }else {
            return ResultEntity.failed("查询失败");
        }
    }


}
